<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{total}} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="openDrawer('add')">
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="stem" label="标题" width="400">
        </el-table-column>
        <el-table-column prop="creator" label="作者">
        </el-table-column>
        <el-table-column prop="likeCount" label="点赞">
        </el-table-column>
        <el-table-column prop="views" label="浏览数">
        </el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="300">
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{row}">
            <div class="actions">
              <i
                class="el-icon-view"
                @click="openDrawer('preview',row.id)"
              ></i>
              <i
                class="el-icon-edit-outline"
                @click="openDrawer('edit',row.id)"
              ></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        :current-page='current'
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
    </el-card>
    <el-drawer
      :before-close="handleClose"
      :visible.sync="isShowDrawer"
      direction="rtl"
      :title="drawerTitle"
      size='50%'
      >
      <div class="demo-drawer__content">
        <h5></h5>
        <div></div>
      </div>
      <el-form ref="form" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input  placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop='content'>
          <quill-editor></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" >确认</el-button>
          <el-button>取 消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { getArticleList, removeArticle } from '@/api/article'
export default {
  name: 'ArticleIndex',
  data () {
    return {
      current: 1,
      pageSize: 10,
      total: 0,
      list: [],
      isShowDrawer: false,
      drawerType: '',
      form: {
        stem: '',
        content: ''
      }
    }
  },
  computed: {
    drawerTitle () {
      let title = ''
      if (this.drawerType === 'add') title = '添加面经'
      if (this.drawerType === 'preview') title = '面经预览'
      if (this.drawerType === 'edit') title = '修改面经'
      return title
    }
  },
  created () {
    this.initData()
  },
  methods: {
    async initData () {
      const res = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      this.list = res.data.rows
      this.total = res.data.total
      // console.log(this.list)
      // console.log(this.total)
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.current = val
      this.initData()
    },
    async del (id) {
      await removeArticle(id)
      // console.log(id)
      // console.log(res)
      this.$message.success('删除成功')
      this.initData()
    },
    openDrawer (type, id) {
      console.log(type, id)
      this.drawerType = type
      this.isShowDrawer = true
    },
    handleClose () {
      this.isShowDrawer = false
    }
  }
}
</script>

<style lang='scss' scoped>
.el-card{
  margin-top: 25px;
  .header{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 16px;
  }
  .actions{
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color:#666;
    >i:hover{
      color: rgba(114,124,245,1);
      cursor: pointer;
    }
  }
}
.el-pagination{
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb{
  margin-top: 10px;
}
</style>
